<?php

require_once 'Metadata.php';

class App_MetadataService
{
	protected $db;
	protected $metadata;
	
	public function __construct()
	{
		$options = array(
			'host' => 'localhost',
			'username' => 'root',
			'password' => '',
			'dbname' => 'xpic'
		);
		
		$this->db = Zend_Db::factory('PDO_MYSQL', $options);
		Zend_Db_Table_Abstract::setDefaultAdapter($this->db);
		
		$this->metadata = new Metadata();
	}
	
	public function updateMetadata($imageID, $name, $val, $metaID = 0)
	{
		if ($metaID == 0)
		{
			$params = array(
				'ImageID' 	=> $imageID,
				'Name'		=> $name,
				'Value'		=> $val
			);
			
			$this->metadata->insert($params);
		}
		else
		{
			$params = array(
				'Name' => $name,
				'Value' => $val
			);
			
			$where = $this->metadata->getAdapter()->quoteInto('MetadataID = ?', $metaID)
												  ->quoteInto('ImageID = ?', $imageID);
			$this->metadata->update($params, $where);
		}
	}
	
	public function getMetadata($imageID)
	{
		$select = $this->metadata->select();
		$select->where('ImageID = ?', $imageID);
		$results = $this->metadata->fetchAll($select);
		
		return $results->toArray();
	}
}